<?php

class User_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
    protected $_name = 'users';
    
    public function LietKeThongTinUser(){
    	//return 'abc';
    	return $this->fetchall()->toArray();
    }
	
    //????????????????????????
    public function getUser($id)
    {
    	$id = (int)$id;
    	$row = $this->fetchRow('id = ' . $id);
    	if (!$row) {
    		echo 'Khong tim thay thong tin User';
    	}
    	return $row->toArray();
    }
    
    //????????????????????????
    public function addUser_old($user_login, $user_pass, $user_name, $user_email, $user_address, $user_phone, $user_registered, $user_status)
    {
    	$data = array(
    			'user_login' => $user_login,
    			'user_pass' => $user_pass,
    			'user_name' => $user_name,
    			'user_email' => $user_email,
    			'user_address' => $user_address,
    			'user_phone' => $user_phone,
    			'user_registered' => $user_registered,
    			'user_status' => $user_status		
    	);
    	$this->insert($data);
    	return $lastID= $this->getAdapter()->lastInsertId();
    	
    }
	
	public function kiemtraemailtontai($email)
	{
		$query=$this->select();
		$query->from('users',array('id'));
		$query->where('user_email=?',$email);
		
		$data = $this->fetchall($query);
		$count = count($data);
		if($count==0)
			return 0;
		return 1;
	}
	
	public function insertuser($firstname, $lastname, $email, $pass, $avata, $sex, $phone, $address)
	{
		$dt = getdate();
		$time = $dt['year'].'-'.$dt['mon'].'-'.$dt['mday'].' '.($dt['hours']+5).':'.$dt['minutes'].':'.$dt['seconds'];
		
		$user_registered = $time;
		$user_status = '1';
		$user_lasttimelogin = $time;
	
		$data = array(
				'user_firstname' => $firstname,
				'user_lastname' => $lastname,
				'user_email' => $email,
				'user_pass' => $pass,
				'user_avata' => $avata,
				'user_sex' => $sex,
				'user_phone' => $phone,
				'user_address' => $address,
				'user_registered' => $user_registered,
				'user_status' => $user_status,
				'user_lasttimelogin' => $user_lasttimelogin,
		);
		$this->insert($data);
    	return $lastID= $this->getAdapter()->lastInsertId();
	}
	
	//????????????????????????
    public function updateUser($id, $user_login, $user_pass, $user_name, $user_email, $user_address, $user_phone)
    {
    	$data = array(
    			'user_login' => $user_login,
    			'user_pass' => $user_pass,
    			'user_name' => $user_name,
    			'user_email' => $user_email,
    			'user_address' => $user_address,
    			'user_phone' => $user_phone,
    	);
    	$this->update($data, 'id = '. (int)$id);
    }
	
	//????????????????????????
    public function deleteUser($id)
    {
    	$this->delete('id =' . (int)$id);
    }
    
	//Kiem tra email va password co trong DB hay ko
    public function checkemailpassword($email, $password){		
		$query=$this->select();
		$query->from('users',array('id','user_firstname','user_lastname','user_avata'));
		$query->where('user_pass =?',$password);
		$query->where('user_email=?',$email);
		
		$row = $this->fetchall($query);
		$info = $row -> toArray();

		$count = count($info);
		if($count==0){
			return 0;
		}
		else{
			$today = date("Y-m-d h:i:s",time());
			$data = array(
				'user_lasttimelogin' => $today);
			$this->update($data, 'ID ='. $info[0]['id']);
			return $info;
		}	
    }
	
	//????????????????????????
	/*
	 public function getemail($user_email){  
    	if($user_email){    			
    		$query=$this->select();
    		$query->from('users',array('ID','user_login','user_name'));
    		$query->where('user_email =?',$user_email);    		
    			
    		$row= $this->fetchall($query);	
			
    		$count = count($row);
    		if($count==0){    			
    			return $row=0;
    		}
    		else{
    			return $row->toArray();
    		}
    	}
    }
	*/
	public function checkemail($email)
	{
		if($email){
			$query = $this->select();
			$query -> from('users', array('id'));
			$query -> where('user_email =?', $email);
			$row = $this->fetchall($query);
			$id = $row -> toArray();
			if(empty($id))
				return 0;
			return $id[0]['id'];
		}else
			return 0;

	}
	
	public function updateUserhash($id)
    {
		$timehash = time(); 
		$prand = rand(0123456789,9876543210); 
		$security = md5($prand);
		$checkrshash= md5($timehash.$id).$security;  
    	$data = array(
    			'timehash' => $timehash,
    			'checkrshash' => $checkrshash  		
    	);
		$this->update($data, 'id = '. (int)$id);
		return $checkrshash;   
    }
	
	public function getUserhash($key)
    {		
		if($key){		
    		$query=$this->select();
    		$query->from('users',array('ID','timehash'));
    		$query->where('checkrshash =?',$key);    		
    			
    		$row= $this->fetchall($query);	
			
    		$count = count($row);
    		if($count==0){    			
    			return $row=0;
    		}
    		else{
    			return $row->toArray();
    		}
    	}
    }
	
	public function checktimehash($timehash)
    {
		if($timehash){    		
			$timenow = time(); 
			if( $timenow - $timehash < 86400 )
			{
				return 1;
			}
			else
			{
				return 0;
			}		    		
    	}
    }
	
	public function updateUserPass($id,$password)
    {
    	$data = array(
				'timehash' => "",
    			'checkrshash' => "",
    			'user_pass' =>$password,
    	);
    	$this->update($data, 'id = '. (int)$id);	
    }
    
    public function get_fname_lname_ava_user($id)
    {
    	$query=$this->select();
    	$query->from('users',array('id','user_firstname','user_lastname','user_avata'));
    	$query->where('ID =?',$id);
  
    	$row= $this->fetchall($query);
    	return $row->toArray();
		//return $query;
    }
	
//lay ban be ngau nhien de hien len trang chu
	public function getuser_friends($iduser){
		$friendships = new Friendship_Model_Dbtable_Friendship();
		$requestfriendship = new Friendship_Model_Dbtable_RequestFriendship();
		
		$fuser = $friendships -> getIDFriends($iduser);
		$ar_all_id_fr = $requestfriendship -> getallidrequestfriendships($iduser);//DS id tat ca request
		
		$d=0;$j=0;$kt=0;
		while($d<6){//Hien thi 6 friend
			$sum = count($ar_friend);// So luong phan tu cua mang, moi lan chay white se thay doi
			$flag = 0;
			$f = $this -> get1user_rand($iduser,$fuser,'friendship_user2',$ar_all_id_fr,'request_friendship_user1');//Lay ngau nhien 1 id khac id friend va khac id request friend
			if($f[0]['id']!="")
			{
				for($i=0; $i<$sum; $i++){
						if($f[0]['id']==$ar_friend[$i]['id']){//Khong hien thi 1 nguoi 2 lan
							$flag = 1;break;
						}
					}
					if($flag ==0){
						$banchung = $friendships -> banchung($iduser,$f[0]['id']);
						$ar_friend[$j]['id']=$f[0]['id'];
						$ar_friend[$j]['user_firstname']=$f[0]['user_firstname'];
						$ar_friend[$j]['user_lastname']=$f[0]['user_lastname'];
						$ar_friend[$j]['user_avata']=$f[0]['user_avata'];
						$ar_friend[$j]['banchung']=$banchung;
						$d++;$j++;
					}
			}
			else break;
			$kt++;
			if($kt==20)$d=6;//Choo chay 20 lan, neu ko tim duoc ban thi thoat ra
		}
		return $ar_friend;
	}

//Lay ngau nhien 1 ban be de thay vao vi tri Close friendships
	public function get1user_friends_rand($iduser){
		$friendships = new Friendship_Model_Dbtable_Friendship();
		$requestfriendship = new Friendship_Model_Dbtable_RequestFriendship();
		
		$fuser = $friendships -> getIDFriends($iduser);//DS id tat ca ban be
		$ar_all_id_fr = $requestfriendship -> getallidrequestfriendships($iduser);//DS id tat ca request
		
		//Truyen vao mang va ten cot, lay ra khac ID va them vao cau try van
		$f = $this -> get1user_rand($iduser,$fuser,'friendship_user2',$ar_all_id_fr,'request_friendship_user1');
		if($f[0]['id']=="") break;
		else{
			$banchung = $friendships -> banchung($iduser,$f[0]['id']);
			$ar_friend['id']=$f[0]['id'];
			$ar_friend['user_firstname']=$f[0]['user_firstname'];
			$ar_friend['user_lastname']=$f[0]['user_lastname'];
			$ar_friend['user_avata']=$f[0]['user_avata'];
			$ar_friend['banchung']=$banchung;
			return $ar_friend;	
		}
		
	}
	
	public function chuoikieukien($a,$m)
	{
		$count = count($a);
		$str_dk = "";
		for($i=0; $i<$count; $i++)
		{
			$str_dk .= ' and id != '.$a[$i][$m];
		}
		return $str_dk;
	}

//lay ngau nhien 1 ban be
	public function get1user_rand($iduser,$array,$m,$array2,$n){
		$dk1 = $this -> chuoikieukien($array,$m);
		$dk2 = $this -> chuoikieukien($array2,$n);
		
		$query=$this->select();
    	$query->from('users',array('id','user_firstname','user_lastname','user_avata'));
    	$query->where("id !=$iduser $dk1 $dk2");
		$query->order('rand()');
		$query->limit(1);
		$row= $this->fetchall($query);
		return $row->toArray();
	}
	

}

